package cn.codesugar.material.design

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import cn.codesugar.material.design.databinding.ActivityBearBinding
import com.bumptech.glide.Glide

class BearActivity : AppCompatActivity() {

    companion object {
        const val BEAR_NAME = "bear_name"
        const val BEAR_IMAGE_ID = "bear_image_id"
    }

    private var b: ActivityBearBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        b = ActivityBearBinding.inflate(layoutInflater)
        setContentView(b?.root)
        val bearName = intent.getStringExtra(BEAR_NAME) ?: ""
        val bearImageId = intent.getIntExtra(BEAR_IMAGE_ID, 0)
        setSupportActionBar(b?.toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        b?.collapsingToolbar?.title = bearName
        b?.bearImageView?.let { Glide.with(this).load(bearImageId).into(it) }
        b?.bearContentText?.text = generateFruitContent(bearName)
    }

    private fun generateFruitContent(bearName: String) = bearName.repeat(500)

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }
}